package com.ruoyi.source.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.ChannelBaseInfo;
import com.ruoyi.db.mapper.ChannelBaseInfoMapper;
import com.ruoyi.laiqianba.service.IHandleCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.metadata.HanaCallMetaDataProvider;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourceBaseInfoMapper;
import com.ruoyi.db.domain.SourceBaseInfo;
import com.ruoyi.source.service.ISourceBaseInfoService;

import javax.annotation.Resource;

/**
 * 线索基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceBaseInfoServiceImpl implements ISourceBaseInfoService 
{
    @Resource
    private SourceBaseInfoMapper sourceBaseInfoMapper;
    @Resource
    ChannelBaseInfoMapper channelBaseInfoMapper;
    @Autowired
    IHandleCacheService handleCacheService;


    /**
     * 查询线索基本信息
     * 
     * @param id 线索基本信息主键
     * @return 线索基本信息
     */
    @Override
    public SourceBaseInfo selectSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoById(id);
    }

    /**
     * 查询线索基本信息列表
     * 
     * @param sourceBaseInfo 线索基本信息
     * @return 线索基本信息
     */
    @Override
    public List<SourceBaseInfo> selectSourceBaseInfoList(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoList(sourceBaseInfo);
    }

    /**
     * 查询线索基本信息列表
     *
     * @param sourceBaseInfo 线索基本信息
     * @return 线索基本信息
     */
    @Override
    public List<Map<String, Object>> selectSourceBaseInfoMapList(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoMapList(sourceBaseInfo);
    }

    @Override
    public List<Map<String, String>> getChannelNameList() {
        return sourceBaseInfoMapper.getChannelNameList();
    }

    /**
     * 新增线索基本信息
     * 
     * @param sourceBaseInfo 线索基本信息
     * @return 结果
     */
    @Override
    public int insertSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {

        ChannelBaseInfo baseInfo = channelBaseInfoMapper.selectChannelBaseInfoByChannelCode(sourceBaseInfo.getChannelCode());

        sourceBaseInfo.setChannelCode(baseInfo.getChannelCode());
        sourceBaseInfo.setChannelName(baseInfo.getChannelName());

        sourceBaseInfo.setApiKey(RandomUtil.randomNumbers(16));
        sourceBaseInfo.setApiSecret(RandomUtil.randomStringUpper(32));

        sourceBaseInfo.setCreateTime(DateUtils.getNowDate());
        return sourceBaseInfoMapper.insertSourceBaseInfo(sourceBaseInfo);
    }

    /**
     * 修改线索基本信息
     * 
     * @param sourceBaseInfo 线索基本信息
     * @return 结果
     */
    @Override
    public int updateSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {

        sourceBaseInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sourceBaseInfoMapper.updateSourceBaseInfo(sourceBaseInfo);
        if (i>0){
            handleCacheService.clearSourceBaseInfo(sourceBaseInfo.getChannelCode());
        }
        return i;
    }

    /**
     * 批量删除线索基本信息
     * 
     * @param ids 需要删除的线索基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoByIds(Long[] ids)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoByIds(ids);
    }

    /**
     * 删除线索基本信息信息
     * 
     * @param id 线索基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoById(id);
    }
}
